package com.wc.算法提高课.A第一章_动态规划.树形DP.二叉苹果树;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/13 10:44
 * @description https://www.acwing.com/problem/content/1076/
 */
public class Main {
    /**
     * 思路：有依赖的背包问题, 相当于把这条边是上的权值下放给指向的苹果, 每一个苹果的体积为1,总体积为Q的分组背包问题
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, M = N << 1, idx = 1;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    // f[i][j] 表示根节点为i的体积为j的最大价值
    static int[][] f = new int[N][N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        dfs(1, -1);
        out.println(f[1][m]);
        out.flush();
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void dfs(int u, int fa) {
        for (int i = h[u]; i > 0; i = ne[i]) {
            int son = e[i];
            if (son == fa) continue;
            dfs(son, u);
            // 分组背包
            for (int j = m; j >= 0; j--) {
                for (int k = 0; k < j; k++) {
                    // 这里为什么是j - k - 1, 因为当前的u->也用了一条边
                    f[u][j] = Math.max(f[u][j], f[u][j - k - 1] + f[son][k] + w[i]);
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}